package cn.zn.oss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.zn.oss.entity.FileInfo;
import cn.zn.oss.mapper.FileInfoMapper;
import cn.zn.oss.service.FileInfoService;
import cn.zn.base.bean.vo.FileInfoEditVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 附件信息表 服务实现类
 * </p>
 *
 * @author zn-admin
 * @since 2021-06-16
 */
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {

    @Override
    @Transactional
    public String insertFileInfos(String attachmentIds, List<FileInfoEditVo> files) {
        // 首先清空已存的附件
        if (StrUtil.isNotBlank(attachmentIds)) {
            List<String> attachmentIdList = StrUtil.split(attachmentIds, ',');
            baseMapper.deleteBatchIds(attachmentIdList);
        }

        if (CollUtil.isNotEmpty(files)) {
            List<String> fileIds = new ArrayList<>();
            files.forEach(item -> {
                FileInfo file = new FileInfo();
                BeanUtil.copyProperties(item, file);
                file.insert();
                fileIds.add(file.getId());
            });
            return CollUtil.join(fileIds, ",");
        }
        return null;
    }

    @Override
    public List<FileInfo> selectFileInfoListByIds(String ids) {
        if (StrUtil.isNotBlank(ids)) {
            String[] idList = StrUtil.split(ids, ",");
            return baseMapper.selectFileInfoListByIds(idList);
        }
        return null;
    }
}
